from datetime import datetime

import scrapy
from scrapy import Selector, Request

from books_rank_crawler.items import BookItem
from books_rank_crawler.settings import FEED_EXPORT_FIELDS_BOOKS_CHINA


class BookschinaSpider(scrapy.Spider):
    name = "bookschina"
    allowed_domains = ["bookschina.com"]
    custom_settings = {
        'FEED_EXPORT_FIELDS': FEED_EXPORT_FIELDS_BOOKS_CHINA
    }

    # 构建url
    def start_requests(self):
        for i in range(1, 35):
            url = f"https://www.bookschina.com/24hour/1_0_{i}/"
            yield Request(url=url)

    def parse(self, response):
        sel = Selector(response)  # 构建选择器
        list_books = sel.css("#container > div > div.listLeft > div.bookList > ul > li")  # 使用css选择器获取books列表
        for list_book in list_books:
            book_item = BookItem()  # 构建BookItem对象

            book_item["image"] = list_book.css("div.cover > a > img::attr(data-original)").get()  # 图片
            book_item["name"] = list_book.css("div.infor > h2 > a::text").get()  # 图书名
            book_item["author"] = list_book.css("div.infor > div.author > a::text").get()  # 作者
            book_item["publisher"] = list_book.css("div.infor > div.publisher > a::text").get()  # 出版社

            # 统计 start_wrap 中 <i class="one"> 的数量
            start_wrap_elements = list_book.css("div.startWrap > i.one")
            book_item["start_wrap"] = len(start_wrap_elements)  # 评分

            book_item["comment"] = list_book.css("div.infor > div.startWrap > a::text").get()  # 评论数

            book_item["sell_price"] = list_book.css("div.infor > div.priceWrap > span.sellPrice::text").get()  # 售价
            book_item["discount"] = list_book.css("div.infor > div.priceWrap > span.discount::text").get()  # 折扣
            book_item["price_tit"] = list_book.css("div.infor > div.priceWrap > del::text").get()  # 定价
            book_item["num"] = list_book.css("div.num > span::text").get()  # 排名
            book_item["crawl_time"] = datetime.now().strftime("%Y%m%d")  # 抓取时间
            book_item["crawl_url"] = response.url  # 抓取url
            yield book_item
